<template>
	<view class="page">
			<view class="job-resume-head">
				<view class="job-resume-back" @click="back()">
					<u-icon name="arrow-left"></u-icon>
				</view>
				<view class="job-resume-search">
					<u-search :show-action="false" borderColor="rgb(230, 230, 230)" searchIcon="/static/job/seach1.png"
						bgColor="#eeeeee" height="60" margin="20rpx" shape="square" placeholder="请输入简历关键词"></u-search>
				</view>
				<view class="job-float-navigation" @click="showFloatNavigation()">
					<image src="../../static/job/nav5.png" mode="aspectFit" class="job-float-navigation-image"></image>
				</view>
			</view>
			<view class="job-resume-screen">
				<HM-filterDropdown ref="filterDropdown" :filterData="filterData" :defaultSelected="defaultSelected"
						:updateMenuName="true" @confirm="confirm" dataFormat="Object"></HM-filterDropdown>
			</view>

		<view class="job-resume-body">
			<view class="job-resume-list">
				<Resume :newResumeList="resumesList"></Resume>
				<u-loadmore :status="loadMoreStatus" loadingIcon="circle" loading-text="努力加载中..." loadmore-text="上拉加载" nomore-text="没有更多信息..." />
			</view>
		</view>
		
		<!-- 浮动导航开始 -->
		<JobFloatNavigation :jobFloatNavigationIsShow="floatNavigation"></JobFloatNavigation>
		<!-- 浮动导航结束 -->
		<!-- 自定义底部导航菜单开始 -->
		<JobTabbar :selectIndex="jobTabbarIndex"></JobTabbar>
		<!-- 自定义底部导航菜单结束 -->
	</view>
</template>

<script>
import JobFloatNavigation from '@/components/JobFloatNavigation/JobFloatNavigation.vue';
import Resume from '@/components/Resume/Resume.vue';
import JobTabbar from '@/components/JobTabbar/JobTabbar.vue';

	export default {
		data() {
			return {
				jobTabbarIndex: 3, //控制底部导航显示那个栏目激活状态
				thisPage: 1, // 当前默认翻页
				thisAllPage: 1, //当前全部页面
				// filterDropdownmenuTop: 110, // 筛选组件初始距离上边变化距离
				// filterDropdownmenuTopStatic: 110, // 筛选组件初始距离上边初始距离
				loadMoreStatus: 'loading', // 底部加载状态
				floatNavigation: false,
				
				resumesList: [], // 简历列表
				
				jobsList: [], // 职位列表
				// indexArr: '',
				// valueArr: '',
				// loadingText: "正在加载...",
				defaultSelected: [], // 筛选默认选择项
				filterData: [], // 组合后的筛选数据
				myAddress: '', // 当前地址
				longitude: '', // 当前经度
				latitude: '', // 当前维度
				coordinate: '', // 经纬度坐标
				defaultCityId: 0 ,// 默认城市ID，默认为不限
				citySelect: [], // 城市选择后传递参数后所选择项目
				filterParameters:{}, // 筛选项目的参数
			};
		},
		filters: {
			// outData(value) {
			// 	return JSON.stringify(value);
			// }
		},
		async onLoad(option) {
			await this.$onLaunched;
			console.log(option)
			
			if(JSON.stringify(option) != "{}") {
				this.defaultCityId = option.cityId
			} else {
				setTimeout(() => {
					//2秒后 修改defaultSelected
					this.changeSelected();
				}, 2000);
			}
			console.log(this.defaultCityId)
			this.readOnLoad() // 加载页面默认数据
	
			// this.filterData = data; 
			//定时器模拟ajax异步请求数据
			// setTimeout(() => {
			// 	this.filterData = data; 
			// }, 100);
	
			//模拟ajax请求子菜单数据。
			// setTimeout(() => {
			// 	this.filterData[2].submenu[0].submenu = [{"name": "附近","value": "附近"},{"name": "1km","value": "1km"},{"name": "2km","value": "2km"},{"name": "3km","value": "3km"},{"name": "4km","value": "4km"},{"name": "5km","value": "5km"}];
			// }, 1000)
	
	
			// setTimeout(() => {
			// 	//2秒后 修改defaultSelected
			// 	this.changeSelected();
			// }, 2000);
		},
		// 下拉刷新
		onPullDownRefresh(){
			this.$refs.filterDropdown.hideMenu(false)
			this.init()
			this.readOnLoad()
			setTimeout(() => {
				uni.stopPullDownRefresh()
				// console.log('停止下拉刷新')
			}, 1000);
			// uni.showToast({
			// 	title: '触发下拉刷新'
			// });
			
		},
		//上拉加载，需要自己在page.json文件中配置"onReachBottomDistance"
		onReachBottom() {
			this.loadMoreStatus = 'loading'
			if(this.thisPage < this.thisAllPage){
				this.thisPage++
				console.log(this.filterParameters)
				if(Object.keys(this.filterParameters).length == 0) {
					this.getJobRecruitList()
				} else {
					this.getJobRecruitScreenList()
				}
			} else {
				this.loadMoreStatus = 'nomore'
			}
			
			// uni.showToast({
			// 	title: '触发上拉加载'
			// });
		
			// let len = this.goodsList.length;
			// if (len >= 80) {
			// 	this.loadingText = "到底了";
			// 	return false;
			// } else {
			// 	this.loadingText = "正在加载...";
			// }
		
		},
		onPageScroll(e) {
			// console.log(e.scrollTop)
		},
		methods: {
			// 初始化页面数据
			init() {
				// this.filterData = [] // 组合后的筛选数据
				this.thisPage = 1  // 当前默认翻页
				this.thisAllPage = 1  //当前全部页面
				this.floatNavigation = false // 顶部浮动菜单隐藏
				this.resumesList = [] // 简历列表
				this.jobsList = []  // 职位列表
			},
			readOnLoad() {
				this.getJobResumesList()
			},
			showFloatNavigation() {
				this.floatNavigation = !this.floatNavigation;
			},
			// 返回上一页面
			back(){
				uni.$u.route({
					type:'navigateBack',
					delta: 1,
				})
			},
			
			
			// 获取页面默认接口数据
			getJobResumesList() {
				this.$api.getJobResumesList({
					openid: getApp().globalData.openid,
					page: this.thisPage
				}).then(res => {
					// console.log(res.data)
					if(this.defaultCityId) {
						this.getJobResumesScreenList({CityID: this.defaultCityId})
					} else {

						this.resumesList = [...this.resumesList, ...res.data.XXList]
						this.$forceUpdate();
					}
					this.thisPage = res.data.CurrentPage
					this.thisAllPage = res.data.Allpage
					// if(!this.jobsList.length) {
						this.loadMoreStatus = 'loadmore'
					// }
					if(this.resumesList.length == 0) {
						this.loadMoreStatus = 'nomore'
					}
					
					if(this.filterData.length == 0) {
						// this.filterData = []
						// 组合地区选项
						let filterDataArea = {
							"name": "地区",
							"type": "hierarchy-column",
							"submenu": [{
								"name": "不限",
								"value": 0,
								"submenu": [{
									"name": "不限城市",
									"value": 0,
								}]
							}],
						}
						// console.log(res.data.DiYuList)
						for (let [indexCity, areaItem] of res.data.DiYuList.entries()) {
							
							let areaItemCity = {
								"name": areaItem.Name,
								"value": areaItem.ID,
								"submenu": []
							}
							// 传递城市ID的选择数组，需要修改默认筛选选项
							if(this.defaultCityId == areaItem.ID) {
								this.citySelect = [indexCity+1, 0]
								setTimeout(() => {
									//2秒后 修改defaultSelected
									this.changeSelected();
								}, 2000);
							}
							// console.log(areaItemCity)
							for (let cityItem of areaItem.ddtype_lst) {
								let areaItemArea = {
									"name": cityItem.dd_name,
									"value": cityItem.dd_id,
								}
								// console.log(areaItemArea)
								areaItemCity.submenu.push(areaItemArea)
							}
							filterDataArea.submenu.push(areaItemCity)
	
						}
						// console.log(filterDataArea)
						this.filterData.push(filterDataArea)
						
						// 组合职位选项
						let filterDataJob = {
							"name": "职位",
							"type": "hierarchy-column",
							"submenu": [{
								"name": "不限",
								"value": 0,
								"submenu": [{
									"name": "不限行业",
									"value": 0,
								}]
							}],
						}
						for (let jobItem of res.data.ZhiWeiList) {
							let jobItemIndustry = {
								"name": jobItem.ClassName,
								"value": jobItem.ID,
								"submenu": []
							}
							// console.log(jobItemIndustry)
							for (let positionItem of jobItem.zwtype_lst) {
								let jobItemPosition = {
									"name": positionItem.zw_name,
									"value": positionItem.zw_id,
								}
								// console.log(jobItemPosition)
								jobItemIndustry.submenu.push(jobItemPosition)
							}
							filterDataJob.submenu.push(jobItemIndustry)
	
						}
						// console.log(filterDataJob)
						this.filterData.push(filterDataJob)
						
						// 组合更多
						let filterDataResumeMore = {
							"name": "更多",
							"type": "radio",
							"submenu": [],
						}
						// 组合类型
						let filterDataResumeType = {
							"name": "类型",
							"value": 0,
							"submenu": [
								{
								"name": "不限",
								"value": 0,
								},
								{
								"name": "全职",
								"value": 1,
								},
								{
								"name": "兼职",
								"value": 2,
								},
							]
						}
	
						filterDataResumeMore.submenu.push(filterDataResumeType)
	
	
						// 组合性别
						let filterDataResumeGender = {
							"name": "性别",
							"value": 0,
							"submenu": [
								{
								"name": "不限",
								"value": 0,
								},
								{
								"name": "男",
								"value": 2,
								},
								{
								"name": "女",
								"value": 1,
								},
							]
						}

						filterDataResumeMore.submenu.push(filterDataResumeGender)
						
						// 组合求职状态
						let filterDataJobStatus = {
							"name": "求职状态",
							"value": 0,
							"submenu": [{
									"name": "不限",
									"value": 0,
								},
								{
									"name": "目前在职",
									"value": 1,
								},
								{
									"name": "已经离职",
									"value": 2,
								},
								{
									"name": "离职中",
									"value": 3,
								}
							]
						}
						filterDataResumeMore.submenu.push(filterDataJobStatus)
						
	
						// 组合学历
						let filterDataResumeEducation = {
							"name": "学历",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}
							
						for (let resumeEducationItem of res.data.XLList) {
							let resumeItemEducation = {
								"name": resumeEducationItem.XLName,
								"value": resumeEducationItem.ID,
							}
							// console.log(jobItemPosition)
							filterDataResumeEducation.submenu.push(resumeItemEducation)
						}
						filterDataResumeMore.submenu.push(filterDataResumeEducation)
						
						// 组合经验
						let filterDataJobExperience = {
							"name": "经验",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}
						
						for (let experienceItem of res.data.GZjyList) {
							let jobItemExperience = {
								"name": experienceItem.JYName,
								"value": experienceItem.ID,
							}
							// console.log(jobItemPosition)
							filterDataJobExperience.submenu.push(jobItemExperience)
						}
						filterDataResumeMore.submenu.push(filterDataJobExperience)
	
						// 刷新时间
						let filterDataResumeRefreshTime = {
							"name": "刷新时间",
							"value": 0,
							"submenu": [{
									"name": "不限",
									"value": 0,
								},
								{
									"name": "一天内",
									"value": 1,
								},
								{
									"name": "一周内",
									"value": 2,
								},
								{
									"name": "两周内",
									"value": 3,
								},
								{
									"name": "一月内",
									"value": 4,
								}
							]
						}
						filterDataResumeMore.submenu.push(filterDataResumeRefreshTime)
						// 加入到筛选中
						this.filterData.push(filterDataResumeMore)
	
					}
					
				})
			},
			
			// 获取页面筛选接口数据
			getJobResumesScreenList(option) {
				// 组合筛选参数
				this.loadMoreStatus = 'loading'
				let screenList = {openid: getApp().globalData.openid, ProID: 1,page: this.thisPage, ...this.filterParameters, ...option}
				this.$api.getJobResumesScreenList(screenList).then(res => {
					// console.log(res.data)
					this.resumesList = [...this.resumesList, ...res.data.XXList]
					this.thisPage = res.data.CurrentPage
					this.thisAllPage = res.data.Allpage
					// if(!this.jobsList.length) {
						this.loadMoreStatus = 'loadmore'
					// }
					if(this.resumesList.length == 0) {
						this.loadMoreStatus = 'nomore'
					}
					
				})
			},
			//接收菜单结果
			confirm(e) {
				// console.log(e)
				let tempFilterParameters = {}
				this.indexArr = e.index;
				this.valueArr = e.value;
				// console.log(this.filterData)
				// console.log(e.value)
				for(let [indexItem, searchItem] of e.value.entries()) {
					// console.log(indexItem, searchItem)
					// indexItem 为0时组合地区
					if(indexItem == 0) {
						if(searchItem[0] != 0) {
							if(searchItem[1] != 0) {
								tempFilterParameters = Object.assign(tempFilterParameters, {CityID: searchItem[1]})
							} else {
								tempFilterParameters = Object.assign(tempFilterParameters, {CityID: searchItem[0]})
							}
							
						}
						continue
					}
					// indexItem 为1时组合职位
					if(indexItem == 1) {
						tempFilterParameters = Object.assign(tempFilterParameters, {JobID1: searchItem[0]}, {JobID2: searchItem[1]})
						continue
					}
					// indexItem 为2时组合更多
					if(indexItem == 2) {
						for(let [indexRequired, requiredItem] of searchItem.entries()) {
							// indexRequired 为 0 时 组合类型
							if(indexRequired == 0) {
								tempFilterParameters = Object.assign(tempFilterParameters, {JobType: requiredItem[0]})
								continue
							}
							
							// indexRequired 为 1 时 组合性别
							if(indexRequired == 1) {
								tempFilterParameters = Object.assign(tempFilterParameters, {Sex: requiredItem[0]})
								continue
							}
							
							// indexRequired 为 2 时 组合求职状态
							if(indexRequired == 2) {
								tempFilterParameters = Object.assign(tempFilterParameters, {QZZT: requiredItem[0]})
								continue
							}
							
							// indexRequired 为 3 时 组合学历
							if(indexRequired == 3) {
								tempFilterParameters = Object.assign(tempFilterParameters, {XuLiID: requiredItem[0]})
								continue
							}
							
							// indexRequired 为 4 时 组合经验
							if(indexRequired == 4) {
								tempFilterParameters = Object.assign(tempFilterParameters, {GZJYID: requiredItem[0]})
								continue
							}
							
							// indexRequired 为 5 时 组合刷新时间
							if(indexRequired == 5) {
								tempFilterParameters = Object.assign(tempFilterParameters, {SXDate: requiredItem[0]})
								continue
							}
						}
						continue
					}
				}
				tempFilterParameters = Object.assign(tempFilterParameters, {Search: ''}) // 添加搜索关键字
				this.filterParameters = tempFilterParameters
				this.resumesList = []
				this.thisPage = 1
				this.thisAllPage = 1
				this.getJobResumesScreenList()
				// console.log(tempFilterParameters)
				// console.log(this.filterParameters)
			},
			//修改选中项-示例
			changeSelected() {
				//设置选中项
				// 一下的注释是针对测试数据说明结构的意思，具体传入什么数据，要看你自己数据。如果data.js数据有修改，注意defaultSelected也要修改
				//传入defaultSelected的结构不能错，错了就报错运行异常。 不想选中的请传入null
	
				this.defaultSelected = [];
				if(this.citySelect.length) {
					this.$nextTick(() => {
						this.defaultSelected = [
							this.citySelect, // this.citySelect是一个数组//第0个菜单选中 一级菜单的第6项，二级菜单的第0项，三级菜单的第1项
							[0, 0], //第1个菜单选中 一级菜单的第2项，二级菜单的第0项，三级菜单的第1项
							[
								[0],
								[0],
								[0],
								[0],
								[0],
								[0]
							], //第2个菜单选中 一级菜单的第1项 一级菜单的第1项
							// [
							// 	[0],
							// 	[1, 2, 7],
							// 	[1, 0]
							// ], //筛选菜单选中 第一个筛选的第0项，第二个筛选的第1,2,7项，第三个筛选的第1,0项
							// [
							// 	[0],
							// 	[0],
							// 	[0]
							// ] //单选菜单选中 第一个筛选的第0项，第二个筛选的第1项，第三个筛选的第1项
						];
					})
				} else {
					this.$nextTick(() => {
						this.defaultSelected = [
							[0, 0], //第0个菜单选中 一级菜单的第6项，二级菜单的第0项，三级菜单的第1项
							[0, 0], //第1个菜单选中 一级菜单的第2项，二级菜单的第0项，三级菜单的第1项
							[
								[0],
								[0],
								[0],
								[0],
								[0],
								[0]
							], //第2个菜单选中 一级菜单的第1项 一级菜单的第1项
							// [
							// 	[0],
							// 	[1, 2, 7],
							// 	[1, 0]
							// ], //筛选菜单选中 第一个筛选的第0项，第二个筛选的第1,2,7项，第三个筛选的第1,0项
							// [
							// 	[0],
							// 	[0],
							// 	[0]
							// ] //单选菜单选中 第一个筛选的第0项，第二个筛选的第1项，第三个筛选的第1项
						];
					})
				}
				
			},
			//轮播图指示器
			swiperChange(event) {
				//this.currentSwiper = event.detail.current;
			},
			// 附近职位列表信息
			getJobRecruitNearbyList(){
				this.loadMoreStatus = 'loading'
				let screenList = {openid: getApp().globalData.openid, ProID: 1,page: this.thisPage, zuobiao: this.coordinate }
				this.$api.getJobRecruitNearbyList(screenList).then(res => {
					// console.log(res.data)
					this.jobsList = [...this.jobsList, ...res.data.XXList]
					this.thisPage = res.data.CurrentPage
					this.thisAllPage = res.data.Allpage
					if(!this.jobsList.length) {
						this.loadMoreStatus = 'nomore'
					}
				})
			},
			// 切换地域范围
			regionalScope(e){
				console.log(e)
				this.init()
				if(e == 'all') {
					this.getJobRecruitList()
				} else {
					this.getJobRecruitNearbyList()
				}
			}
		},
		components: {
			JobFloatNavigation,
			Resume,
			JobTabbar,
		}
	}
</script>

<style lang="scss" scoped>
@import url('jobResume.css');
</style>
